//
//  BaseViewController.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/14.
//

#import "BaseViewController.h"

@interface BaseViewController ()
@property (nonatomic,strong) UIImageView * bgView;
@property (nonatomic,strong) NavView * navgationView;
@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initConfig];
    [self setBolthStae];
}

- (void)initConfig
{
    [self.view addSubview:self.bgView];
    if (@available(iOS 11.0,*)) {
        UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self.view addSubview:self.navgationView];
    
    if ([NSStringFromClass(self.class) isEqualToString:@"HomeVC"] ||[NSStringFromClass(self.class) isEqualToString:@"SleepVC"] ||[NSStringFromClass(self.class) isEqualToString:@"EnergyVC"] ||[NSStringFromClass(self.class) isEqualToString:@"MeVC"]
        ||[NSStringFromClass(self.class) isEqualToString:@"LoginVC"] ) {
        self.navgationView.backBtn.hidden = YES;
    }
   
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getBleConnect];
}

-(void)setBolthStae{
    //监听设备连接状态
    [[BCLRingManager shared] setupConnectionStateMonitorWithStateChanged:^(BOOL state) {
        if (!state) {
            FhudShowStr(@"设备已断开")
            NSDictionary * info = @{@"state":@(state)};
            [[NSNotificationCenter defaultCenter] postNotificationName:BatteryLevelNotification object:info];
            return;
        }
        //同步时区
        [[BCLRingManager shared] syncDeviceTimeWithTimeZoneWithDate:[NSDate date]
                                                           timeZone:RingTimeZoneEast8
                                                         completion:^(BOOL success, NSString *_Nullable message) {
            if (success) {
                [[NSNotificationCenter defaultCenter] postNotificationName:ObtainHistoricalDataNotification object:nil];
                }
            else
            {
                [BDLog info:[NSString stringWithFormat:@"同步时间失败：%@", message]];
            }
        }];
    }];
}

-(void)getBleConnect{
    
    [[BCLRingManager shared] getBlcConnectWithCompletion:^(BOOL state) {
        NSDictionary * info = @{@"state":@(state)};
        [[NSNotificationCenter defaultCenter] postNotificationName:BatteryLevelNotification object:info];
    }];
}

- (void)setTitle:(NSString *)title
{
    [super setTitle:title];
    self.navgationView.titleText = title;
}

- (void)backBtnClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (NavView*)navgationView
{
    if (!_navgationView) {
        _navgationView = [[NavView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, NavHeight)];
        _navgationView.backgroundColor = UIColor.clearColor;
        [_navgationView.backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _navgationView;
}
-(UIImageView *)bgView{
    
    if (!_bgView) {
        _bgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        _bgView.image = [UIImage imageNamed:@"Home_bg_icon"];
    }
    return _bgView;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}

- (void)dealloc
{
    NSLog(@"页面释放了------------------- %@",NSStringFromClass([self class]));
}




@end
